/*
>> PixelToPixelOp.s
>>
>> Pixel operation skeleton, allowing buffer and scalar inputs, and
>> buffer outputs. Adapted from PixelToPixelOp.py.
>>
>> Author: Wouter Caarls
>> Date:   06-04-2005
*/

#include "stdstratego.h"
#include "stdsc.h"

#include "pixelbypixel.h"

void PixelToPixelOp(argcode_t *argcode, int arguments, code body)
{
  int ii;

  for (ii=0; ii < arguments; ii++)
  {
    if (argcode[ii].argtype == 0 || argcode[ii].argtype == 1)
    {
      body = replace(`*$@argcode[ii].id`, argcode[ii].id, body);

      @declare(argcode[ii].datatype, argcode[ii].id);

      argcode[ii].state.buffer = 1;
      argcode[ii].state.state = 0;
      argcode[ii].state.delay = 0;
    }
    else
    {
      @declare(addrpointer(argcode[ii].datatype), argcode[ii].id);
      if (@dataclass(argcode[ii].datatype) == 0)
        @argcode[ii].id = &(@argcode[ii].argid).data.d;
      else if (@dataclass(argcode[ii].datatype) == 1)
        @argcode[ii].id = &(@argcode[ii].argid).data.f;
      else
        @argcode[ii].id = @cast(addpointer(argcode[ii].datatype), (@argcode[ii].argid).data.b);

      argcode[ii].state.buffer = 1;
      argcode[ii].state.state = 0;
      argcode[ii].state.delay = 0;
    }
  }

  repeat {
    $@body;
  };

  epilogue {
    int ii;

    for (ii=0; ii < arguments; ii++)
    {
      if (argcode[ii].argtype == 0)
        BufferRemoveReference(ii);
      else if (argcode[ii].argtype == 1)
        BufferFinalize(ii);
    }
  };
}
